package cn.lg.soar.flow.biz.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.database.util.PageUtils;
import cn.lg.soar.flow.api.constant.ModuleConst;
import cn.lg.soar.flow.api.model.*;
import cn.lg.soar.flow.api.service.IFlowTaskApi;
import cn.lg.soar.flow.biz.model.DoneQuery;
import cn.lg.soar.flow.biz.model.InstanceQuery;
import cn.lg.soar.flow.biz.model.InstanceVO;
import cn.lg.soar.flow.biz.model.TodoQuery;
import cn.lg.soar.flow.biz.service.FlowInstanceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程实例管理
 * @author luguoxiang
 * @since 2025-08-02
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Tag(name = "流程实例管理")
@Validated
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/instance")
public class FlowInstanceController {

    @Autowired
    private IFlowTaskApi flowTaskApi;
    @Autowired
    private FlowInstanceService flowInstanceService;

    @Operation(summary = "发起流程")
    @RequestMapping(path = "start", method = RequestMethod.POST)
    public HttpResult<FlowInfoDTO> start(@RequestBody StartFlowDTO dto) {
        return HttpResult.data(flowTaskApi.startFlow(dto));
    }

    @Operation(summary = "完成任务")
    @RequestMapping(path = "complete", method = RequestMethod.POST)
    public HttpResult<FlowInfoDTO> complete(@RequestBody CompleteTaskDTO dto) {
        return HttpResult.data(flowTaskApi.completeTask(dto));
    }

    @Operation(summary = "撤销流程")
    @RequestMapping(path = "revoke", method = RequestMethod.POST)
    public HttpResult<Void> revoke(@RequestBody RevokeDTO revoke) {
        flowTaskApi.revoke(revoke);
        return HttpResult.success();
    }

    @Operation(summary = "流程列表")
    @RequestMapping(path = "page", method = RequestMethod.GET)
    public HttpResult<PageResult<InstanceVO>> page(PageQuery pageQuery, InstanceQuery query) {
        IPage<InstanceVO> page = flowInstanceService.page(PageUtils.toPage(pageQuery), query);
        return HttpResult.data(PageUtils.toPageResult(page));
    }

    @Operation(summary = "我发起的")
    @RequestMapping(path = "started", method = RequestMethod.GET)
    public HttpResult<PageResult<InstanceVO>> started(PageQuery pageQuery, InstanceQuery query) {
        IPage<InstanceVO> page = flowInstanceService.started(PageUtils.toPage(pageQuery), query);
        return HttpResult.data(PageUtils.toPageResult(page));
    }

    @Operation(summary = "我的待办")
    @RequestMapping(path = "todo", method = RequestMethod.GET)
    public HttpResult<PageResult<TodoDTO>> todo(PageQuery pageQuery, TodoQuery query) {
        IPage<TodoDTO> list = flowInstanceService.todo(PageUtils.toPage(pageQuery), UserContext.getUserId(), query);
        return HttpResult.data(PageUtils.toPageResult(list));
    }

    @Operation(summary = "我的已办")
    @RequestMapping(path = "done", method = RequestMethod.GET)
    public HttpResult<PageResult<DoneDTO>> done(PageQuery pageQuery, DoneQuery query) {
        IPage<DoneDTO> list = flowInstanceService.done(PageUtils.toPage(pageQuery), UserContext.getUserId(), query);
        return HttpResult.data(PageUtils.toPageResult(list));
    }

    @Operation(summary = "任务详情")
    @RequestMapping(path = "taskDetail", method = RequestMethod.GET)
    public HttpResult<TaskDetailDTO> taskDetail(@RequestParam("taskId") String taskId) {
        return HttpResult.data(flowTaskApi.taskDetail(taskId));
    }

    @Operation(summary = "流程进度")
    @RequestMapping(path = "progress", method = RequestMethod.GET)
    public HttpResult<ProgressVO> progress(@RequestParam("procInstId") String procInstId) {
        return HttpResult.data(flowInstanceService.progress(procInstId));
    }

    @Operation(summary = "审批记录")
    @RequestMapping(path = "auditLog", method = RequestMethod.GET)
    public HttpResult<List<AuditLogDTO>> auditLog(@RequestParam("procInstId") String procInstId) {
        return HttpResult.data(flowTaskApi.auditLogList(procInstId));
    }

    @Operation(summary = "审批记录2")
    @RequestMapping(path = "auditLog2", method = RequestMethod.GET)
    public HttpResult<List<AuditLogDTO>> auditLog2(@RequestParam("flowCode") String flowCode, @RequestParam("bizId") String bizId) {
        return HttpResult.data(flowTaskApi.auditLogList(flowCode, bizId));
    }

}